package com.js.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.js.pojo.Student;
import com.js.pojo.Teacher;

import com.js.service.TeacherService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;
    @RequestMapping("/queryAll")
    @ResponseBody
    public String queryAllTeacher(){
        List<Teacher> teacherList = teacherService.queryAllTeacher();
        System.out.println(teacherList);
        return teacherList.toString();
    }
    @RequestMapping("/toAdd")
    public String toAddTeacher(){
        return "teacher/addTeacher";
    }
    @RequestMapping("/toAdd1")
    public String toAddTeacher1(){
        return "teacher/registerTeacher";
    }
    @RequestMapping("/add")
    public String addTeacher(Teacher teacher){
        System.out.println("addTeacher==>"+teacher.toString());
        int i = teacherService.addTeacher(teacher);
        System.out.println(i);
        if (i>0){
            return "redirect:/teacher/queryTeacherByPage";
        }else {
            return "redirect:/teacher/toAdd";
        }
    }
    @RequestMapping("/add1")
    public String addTeacher1(Teacher teacher){
        System.out.println("addTeacher==>"+teacher.toString());
        int i = teacherService.addTeacher(teacher);
        System.out.println(i);
        if (i>0){
            return "teacher/teacherLogin";
        }else {
            return "redirect:/teacher/toAdd1";
        }
    }
    @RequestMapping("/dele/{id}")
   public String deleteTeacherById(@PathVariable("id") int id){
        int i = teacherService.deleteTeacherById(id);
        System.out.println(i);
        if (i>0){
            System.out.println("删除成功！！");
        }
        return "redirect:/teacher/queryTeacherByPage";
    }
    @RequestMapping("/toUpdate")
    public String toUPdateTeacher(int id,Model model){
        Teacher teacher = teacherService.queryTeacherById(id);
        model.addAttribute("QTeacher",teacher);
        return "teacher/updateTeacher";
    }
    //存session 数据 不只跳转
    @RequestMapping("/toUpdate1")
    public String toUPdateTeacher1(){
        return "teacher/updateTeacher1";
    }
    @RequestMapping("/update")
    public String updateTeacher(Teacher teacher,Model model){
        System.out.println("updateTeacher==>"+teacher.toString());
        int i = teacherService.updateTeacher(teacher);
        System.out.println(i);
        if (i>0){
            return "redirect:/teacher/queryTeacherByPage";
        }else {
            return "redirect:/teacher/toUpdate";
        }
    }
    @RequestMapping("/update1")
    public String updateTeacher1(Teacher teacher,Model model){
        System.out.println("updateTeacher==>"+teacher.toString());
        int i = teacherService.updateTeacher(teacher);
        System.out.println(i);
        if (i>0){
            return "updateSuccess";
        }
        return "redirect:/teacher/toUpdate1";
    }
    @RequestMapping("/queryId")
    @ResponseBody
    public String queryTeacherById(){
        Teacher teacher = teacherService.queryTeacherById(1);
        System.out.println(teacher);
        return teacher.toString();
    }
    @ResponseBody
    @RequestMapping("/queryAccount")
    public String queryTeacherByAccount(){
        Teacher teacher = teacherService.queryTeacherByAccount("1001");
        System.out.println(teacher);
        return teacher.toString();
    }
    @RequestMapping("/queryName")
    public String queryTeacherByName(String teacherName, Model model){
        System.out.println("要查询的老师:" + teacherName);
        List<Teacher> teachers = teacherService.queryTeacherByName(teacherName);
        PageHelper.startPage(1,3);
        PageInfo<Teacher> pageInfo = new PageInfo<>(teachers);
        model.addAttribute("pageTeacher",pageInfo);
        return "teacher/teacherList";
    }
    /***
     * 分页
     * @param model
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping("/queryTeacherByPage")
    public String queryBookByPage(Model model,
                                  @RequestParam(required = true,defaultValue = "1") Integer page,
                                  @RequestParam(required = false,defaultValue = "3")Integer pageSize){
        //page默认值是1，pageSize默认是10，意思是从第1页开始，每页显示10条记录。
        PageHelper.startPage(page,pageSize);
        List<Teacher> teachersList = teacherService.queryAllTeacher();
        PageInfo<Teacher> pageInfo = new PageInfo<>(teachersList);
        model.addAttribute("pageTeacher",pageInfo);
        return "teacher/teacherList";
    }
}
